package xyf.p04.tree;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: P0590_NPostOrder
 * Description:
 * Author: xyf
 * Date: 9/4/22 9:21 AM
 * Version: 1.0
 **/
public class P0590_NPostOrder
{
    class Node
    {
        public int val;
        public List<Node> children;

        public Node()
        {
        }

        public Node(int _val)
        {
            val = _val;
        }

        public Node(int _val, List<Node> _children)
        {
            val = _val;
            children = _children;
        }

        public List<Integer> postorder(Node root)
        {
            List<Integer> result = new ArrayList<>();
            post(result, root);
            return result;
        }

        private void post(List<Integer> result, Node root)
        {
            if (root == null)
            {
                return;
            }

            List<Node> children = root.children;
            for (Node child : children)
            {
                post(result, child);
            }
            result.add(root.val);
        }
    }
}